//
//  GFunTVC.m
//  guess
//
//  Created by fank on 15/11/8.
//  Copyright © 2015年 fank. All rights reserved.
//

#import "GFunTVC.h"
#import "Define.h"
#import "GHttpEnqine.h"
#import <UIImageView+WebCache.h>
#import "PhotoModel.h"
#import "PhotoItemView.h"
#import "CoreSVP.h"



@interface GFunTVC()
{
    YouYiSiSingleDataModel * _singleData;
    UIView *  _allView;
    UIView  * _topView;
    UILabel * _titleLab;
    UILabel * _timeLab;
    
    UILabel * _textLab;
    
    UIView  * _bottomView;
    UIButton *	_transmitBtn;
    UIButton *  _saveBtn;
    CGRect _frame;
    JokeType _jokeType;
}
@property  UIImage * myImage;

@end



@implementation GFunTVC



-(instancetype)initWithData:(YouYiSiSingleDataModel  *)singleData;
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    _frame = self.frame;
    _singleData =singleData;
    
    _topView=[[UIView alloc]initWithFrame:CGRectZero];
    _titleLab=[[UILabel alloc]initWithFrame:CGRectZero];
    _timeLab=[[UILabel alloc]initWithFrame:CGRectZero];
    
    _imageImg=[[UIImageView alloc]initWithFrame:CGRectZero];
    _saveBtn =[[UIButton alloc]initWithFrame:CGRectZero];
    _bottomView=[[UIView alloc]initWithFrame:CGRectZero];
    _transmitBtn=[[UIButton alloc]initWithFrame:CGRectZero];
    _allView =[[UIView alloc]initWithFrame:CGRectZero];
    
    
    [self addSubview:_allView];
    _jokeType = JokeImage;
    return self;
}

-(void)setSpitslotImage:(UIImage *)img
{
    _myImage =img;
}

+(CGSize)heightWithImage:(UIImage *)img
{
    return img.size;
}

-(void)layoutSubviews
{
    [super layoutSubviews];

        _allView.frame = CGRectMake(0, 0,kscreenWith-20 ,self.frame.size.height-15);
        _allView.layer.cornerRadius =10;
        _allView.backgroundColor =[UIColor whiteColor];
        
        _topView.frame = CGRectMake(0, 0,(kscreenWith-20) , 30);
        _topView.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
        _topView.layer.cornerRadius =10;
        [_allView addSubview:_topView];
        
        
        _titleLab.frame =CGRectMake(5, 5, kscreenWith-190, 25);
        _titleLab.font =[UIFont fontWithName:@"" size:12];
        _titleLab.textColor =[UIColor blackColor];
        _titleLab.text =_singleData.title;
        [_topView addSubview:_titleLab];
        
        _timeLab.frame =CGRectMake(kscreenWith-180, 5, 170, 25);
        _timeLab.font =[UIFont fontWithName:@"" size:12];
        _timeLab.text =_singleData.date;
        _timeLab.textColor =[UIColor blackColor];
        [_topView addSubview:_timeLab];
        
        
        
        
        
        
        _imageImg.frame =CGRectMake(0, 31, kscreenWith -20 , _myImage.size.height/_myImage.size.width*(kscreenWith-20));
        _imageImg.backgroundColor =[UIColor whiteColor];
        [_allView addSubview:_imageImg];
        
        _imageImg.image =_myImage;
        
    
    //开启事件
    _imageImg.userInteractionEnabled = YES;
    
    //模式
    _imageImg.contentMode=UIViewContentModeScaleAspectFill;
    
    _imageImg.clipsToBounds = YES;
    
    //添加手势
    [_imageImg addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchImage:)]];

    
    
    
    
        _bottomView.frame =CGRectMake(0, _myImage.size.height/_myImage.size.width*(kscreenWith-20)+32, kscreenWith-20, 40);
    _bottomView.backgroundColor = [UIColor lightGrayColor];;
    [_allView addSubview:_bottomView];
    
    _transmitBtn.frame =CGRectMake(0, 1, kscreenWith/2-10-0.5, 39);
    _transmitBtn.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
    [_transmitBtn setTitle:@"分享" forState:UIControlStateNormal];
    [_transmitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [_transmitBtn addTarget:self action:@selector(shareBtn) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_transmitBtn];
    
    
    _saveBtn.frame =CGRectMake(kscreenWith/2-10+0.5, 1, kscreenWith/2-10-0.5, 39);
    _saveBtn.backgroundColor =UIColorFromRGBValue(0xF6F6F6);
    [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [_saveBtn addTarget:self action:@selector(savePhoto) forControlEvents:UIControlEventTouchDown];
    [_saveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_saveBtn addTarget:self action:@selector(savePhoto) forControlEvents:UIControlEventTouchDown];
    [_bottomView addSubview:_saveBtn];
    }
-(void)touchImage:(UITapGestureRecognizer *)tap{
    if(_ClickImageBlock != nil) _ClickImageBlock(_singleData);
}

#pragma mark - save photo
- (void)savePhoto{
    
    
    
    PhotoModel *pbModel=[[PhotoModel alloc] init];
    pbModel.mid = (NSUInteger )self.index+1;
    
    pbModel.title = [NSString stringWithFormat:@"%@",[_singleData.date substringFromIndex:18]];
    pbModel.desc = [NSString stringWithFormat:@"%@",_singleData.title];
    pbModel.image_HD_U = _singleData.imageUrl;
    
    //取出对应模型
    PhotoModel *itemModel = (PhotoModel *)pbModel;
    
    //取出itemView
    PhotoItemView * itemImage = [[PhotoItemView alloc]init];
    itemImage.photoModel =itemModel;
    
    if(!itemImage.hasImage){
        [CoreSVP showSVPWithType:CoreSVPTypeError Msg:@"无图片数据" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
        return;
    }
    
    //读取缓存
    if([itemModel read]){//已经保存过本地
        
        [CoreSVP showSVPWithType:CoreSVPTypeInfo Msg:@"图片已存" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
    }else{
        
        //展示提示框
        [CoreSVP showSVPWithType:CoreSVPTypeLoadingInterface Msg:@"保存中" duration:0 allowEdit:NO beginBlock:nil completeBlock:nil];
        
        [itemImage save:^{
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [CoreSVP showSVPWithType:CoreSVPTypeSuccess Msg:@"保存成功" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
                
                //保存记录
                [itemModel save];
            });
            
            
        } failBlock:^{
            
            [CoreSVP showSVPWithType:CoreSVPTypeError Msg:@"保存失败" duration:1.0f allowEdit:NO beginBlock:nil completeBlock:nil];
        }];
    }
}


-(void)shareBtn
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:_imageImg.image forKey:@"image"];
    [dict setObject:_singleData.title forKey:@"title"];
    [self shareFunWithShareInfo1:dict];
}


//分享方法
-(void)shareFunWithShareInfo1:(NSDictionary *)dictionary
{
    BOOL isInstallWeChat = [WXApi isWXAppInstalled];
    BOOL isInstallQQ    = [QQApiInterface isQQInstalled];
    
    NSMutableArray *array = [NSMutableArray array];
    if (isInstallWeChat == YES) {
        [array addObject:UMShareToWechatFavorite];
        [array addObject:UMShareToWechatSession];
        [array addObject:UMShareToWechatTimeline];
    }
      [array addObject:UMShareToSina];
    if (isInstallQQ == YES) {
        [array addObject:UMShareToQQ];
       // [array addObject:UMShareToQzone];
    }
    if (dictionary) {
            UIImage *image = [dictionary objectForKey:@"image"];
            NSString *title = [dictionary objectForKey:@"title"];
            //微信收藏
            [UMSocialData defaultData].extConfig.wechatFavoriteData.shareImage = image;
            [UMSocialData defaultData].extConfig.wechatFavoriteData.title = title;
            [UMSocialData defaultData].extConfig.wechatFavoriteData.wxMessageType = UMSocialWXMessageTypeImage;
            //微信好友
            [UMSocialData defaultData].extConfig.wechatSessionData.shareImage = image;
            [UMSocialData defaultData].extConfig.wechatSessionData.title = title;
            [UMSocialData defaultData].extConfig.wechatSessionData.wxMessageType = UMSocialWXMessageTypeImage;
            //微信朋友圈
            [UMSocialData defaultData].extConfig.wechatTimelineData.shareImage = image;
            [UMSocialData defaultData].extConfig.wechatTimelineData.title = title;
            [UMSocialData defaultData].extConfig.wechatTimelineData.wxMessageType = UMSocialWXMessageTypeImage;
            //新浪微博
            [UMSocialData defaultData].extConfig.sinaData.shareImage = image;
            //QQ
            [UMSocialData defaultData].extConfig.qqData.shareImage = image;
            [UMSocialData defaultData].extConfig.qqData.title = title;
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeImage;
            //QQ空间
//            [UMSocialData defaultData].extConfig.qzoneData.shareImage = image;
//            [UMSocialData defaultData].extConfig.qzoneData.title = title;
        
            [UMSocialSnsService presentSnsIconSheetView:self.selfVC appKey:UmengAppkey shareText:title shareImage:nil shareToSnsNames:array delegate:self.selfVC];
        }
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

